import { RouterItem } from "../redux/reducer";

// 白名单路径
const whiteList = ["/", "/login", "/register"];

export function useCheckPermission(pathname: string, roleList: RouterItem[]) {
  if (whiteList.includes(pathname)) return true;
  const permissionResult = checkPermission(pathname, roleList);
  return permissionResult;
}

// 权限检查函数
function checkPermission(pathname: string, routes: RouterItem[]): boolean {
  const stack = [...routes];
  while (stack.length > 0) {
    const item = stack.pop();
    if (item?.key === pathname) return true;
    if (item?.children) stack.push(...item.children);
  }
  return false;
}
